package com.zhu.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashUtil {

    // 计算 md5(sha1(passwd + salt))
    public static String md5OfSha1(String passwd, String salt) {
        try {
            // 组合密码和盐
            String combined = passwd + salt;

            // 计算 SHA-1 哈希
            MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");
            byte[] sha1Hash = sha1Digest.digest(combined.getBytes());

            // 将 SHA-1 的字节数组转换为十六进制字符串
            StringBuilder sha1HexString = new StringBuilder();
            for (byte b : sha1Hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    sha1HexString.append('0');
                }
                sha1HexString.append(hex);
            }

            // 计算 MD5 哈希
            MessageDigest md5Digest = MessageDigest.getInstance("MD5");
            byte[] md5Hash = md5Digest.digest(sha1HexString.toString().getBytes());

            // 将 MD5 的字节数组转换为十六进制字符串
            StringBuilder md5HexString = new StringBuilder();
            for (byte b : md5Hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    md5HexString.append('0');
                }
                md5HexString.append(hex);
            }

            return md5HexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    /*public static void main(String[] args) {
        String passwd = "corrdata123"; // 你的密码
        String salt = "corrosion";        // 你的盐

        String hashedPassword = md5OfSha1(passwd, salt);
        System.out.println("Hashed Password: " + hashedPassword);
    }*/
}

